home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / PLAUGER.ZIP / MEMORY
Text File  |  1996-03-25  |  3KB  |  106 lines

  1. --------------- Listing 1: <memory>, part 1 -------------
  2.  
  3. <f>// memory standard header
  4. #ifndef _MEMORY_
  5. #define _MEMORY_
  6. #include <cstdlib>
  7. #include <iterator>
  8. #include <new>
  9. #include <utility>
  10. ///namespace std {
  11.  
  12. #ifndef _FARQ    /* specify standard memory model */
  13.  #define _FARQ
  14.  #define _PDFT    ptrdiff_t
  15.  #define _SIZT    size_t
  16. #endif
  17.  
  18.         // TEMPLATE FUNCTION _Allocate
  19. template<class T> inline
  20.     T _FARQ *_Allocate(_PDFT n, T _FARQ *)
  21.     {if (n < 0)
  22.         n = 0;
  23.     return ((T _FARQ *)operator new((_SIZT)n * sizeof (T))); }
  24.  
  25.         // TEMPLATE FUNCTION _Construct
  26. template<class T1, class T2> inline
  27.     void _Construct(T1 _FARQ *p, const T2& val)
  28.     {new ((void _FARQ *)p) T1(val); }
  29.  
  30.         // TEMPLATE FUNCTION _Destroy
  31. template<class T> inline
  32.     void _Destroy(T _FARQ *p)
  33. ///    {p->~T(); }
  34.     {p->T::~T(); }    ///
  35.  
  36. inline void _Destroy(char _FARQ *_P)
  37.     {}
  38. inline void _Destroy(wchar_t _FARQ *_P)
  39.     {}
  40.  
  41.         // (TEMPLATE) CLASS allocator
  42. template<class T>
  43.     class allocator {
  44. public:
  45.     typedef _SIZT size_type;
  46.     typedef _PDFT difference_type;
  47.     typedef T _FARQ *pointer;
  48.     typedef const T _FARQ *const_pointer;
  49.     typedef T _FARQ& reference;
  50.     typedef const T _FARQ& const_reference;
  51.     typedef T value_type;
  52.     pointer address(reference x) const
  53.         {return (&x); }
  54.     const_pointer address(const_reference x) const
  55.         {return (&x); }
  56. ///    template<class U>
  57. ///        struct rebind {
  58. ///            typedef allocator<U> other;
  59. ///            };
  60. ///    allocator()
  61. ///        {}
  62. ///    template<class _U>
  63. ///        allocator(const allocator<_U>&)
  64. ///        {}
  65. ///    template<class _U>
  66. ///        allocator<_T>& operator=(const allocator<_U>&)
  67. ///        {}
  68.     pointer allocate(size_type n, const void *)    ///
  69.         {return (_Allocate((difference_type)n,    ///
  70.             (pointer)0)); }    ///
  71.     char _FARQ *Charalloc(size_type _N)    ///
  72.         {return (_Allocate((difference_type)_N,    ///
  73.             (char _FARQ *)0)); }    ///
  74.     void deallocate(void _FARQ *p)
  75.         {operator delete(p); }
  76.     void construct(pointer p, const T& val)
  77.         {_Construct(p, val); }
  78.     void destroy(pointer p)
  79.         {_Destroy(p); }
  80.     _SIZT max_size() const
  81.         {_SIZT n = (_SIZT)(-1) / sizeof (T);
  82.         return (0 < n ? n : 1); }
  83.     };
  84.  
  85.         // CLASS allocator<void>
  86. class allocator<void> {
  87. public:
  88.     typedef void T;
  89.     typedef T _FARQ *pointer;
  90.     typedef const T _FARQ *const_pointer;
  91.     typedef T value_type;
  92. ///    template<class U>
  93. ///        struct rebind {
  94. ///            typedef allocator<U> other;
  95. ///            };
  96.     };
  97.  
  98.         // OPERATORS FOR allocator
  99. template<class T, class U>
  100.     bool operator==(const allocator<T>&, const allocator<U>&)
  101.     {return (true); }
  102. ///template<class _T, class _U>
  103. ///    bool operator!=(const allocator<T>&, const allocator<U>&)
  104. ///       {return (false); }<d>
  105.  
  106.